﻿Public Class frmMTipousuario
    Private frmRTipousuario As New frmTipousuario
    Public arrConsulta(3) As String
    Private arrCampos() As String = {"tipousuario_id", "tipousuario_nombre", "tipousuario_descri", "tipousuario_nivel"}

    Private Sub frmTipousuario_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        refreshList()

        Dim dstRecord As New DataSet
        dstRecord.Tables.Add("tipousuario")
        mdlCRUD.loadRecord(dstRecord, "tipousuario", "tipousuario", "", Nothing)
        frmRTipousuario.frmParent = Me

        'frmRTipoelemento.cmbTipoelemento.DataSource = dstRecord.Tables("Departamento")
        'frmRTipoelemento.cmbTipoelemento.DisplayMember = dstRecord.Tables("Departamento").Columns(1).Caption.ToString
        'frmRTipoelemento.cmbTipoelemento.ValueMember = dstRecord.Tables("Departamento").Columns(0).Caption.ToString
    End Sub

    Public Sub refreshList()
        Dim dstRecord As New DataSet
        Dim strCondicion As String = ""
        dstRecord.Tables.Add("tipousuario")

        For i As Integer = 0 To 2
            If (Me.arrConsulta(i) <> "") Then
                strCondicion &= If(strCondicion <> "", " AND ", "") & If(i = 2, Me.arrCampos(i) & " = '" & LCase(Me.arrConsulta(i)) & "'", "LOWER(" & Me.arrCampos(i) & "::text) LIKE '%" & LCase(Me.arrConsulta(i)) & "%'")
            End If
        Next

        mdlCRUD.loadRecord(dstRecord, "tipousuario", "tipousuario", strCondicion, Nothing)

        Me.dgvTipousuario.DataSource = dstRecord.Tables("tipousuario")
        Me.dgvTipousuario.Columns(0).HeaderText = "Codigo"
        Me.dgvTipousuario.Columns(1).HeaderText = "Nombre"
        Me.dgvTipousuario.Columns(2).HeaderText = "Descripción"
        Me.dgvTipousuario.Columns(3).HeaderText = "Nivel"
        'Me.dgvTipoelemento.Columns(3).HeaderText = "tipoelemento"
        Me.dgvTipousuario.Columns(0).Width = 60
        Me.dgvTipousuario.Columns(1).Width = 260
        Me.dgvTipousuario.Columns(2).Width = 260
        Me.dgvTipousuario.Columns(3).Width = 260
    End Sub

    Private Sub tsbEditar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbEditar.Click
        Dim strId As String = Me.dgvTipousuario.Rows(Me.dgvTipousuario.CurrentRow.Index).Cells(0).Value.ToString()

        If (strId <> "") Then
            frmRTipousuario.modeForm = 3
            frmRTipousuario.Text = "Editar Tipo de Usuario"
            frmRTipousuario.ShowDialog()
        Else
            MsgBox("Debe seleccionar un registro", MsgBoxStyle.Information, "Tipo de Usuario")
        End If
    End Sub

    Private Sub tsbNuevo_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbNuevo.Click
        frmRTipousuario.modeForm = 1
        frmRTipousuario.Text = "Nuevo Tipo de Usuario"
        frmRTipousuario.ShowDialog()
    End Sub

    Private Sub tsbRefresh_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbRefresh.Click
        arrConsulta(0) = ""
        arrConsulta(1) = ""
        arrConsulta(2) = ""
        Me.refreshList()

    End Sub

    Private Sub tsbConsultar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbConsultar.Click
        frmRTipousuario.modeForm = 2
        frmRTipousuario.Text = "Consultar Tipo de Usuario"
        frmRTipousuario.ShowDialog()
    End Sub

    Private Sub tsbBorrar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbBorrar.Click
        Dim objResult = MsgBox("Esta seguro que desea borrar el registro seleccionado?", MsgBoxStyle.YesNo, "Borrar Tipo de Usuario")

        If (objResult = DialogResult.Yes) Then
            Dim strId As String = Me.dgvTipousuario.Rows(Me.dgvTipousuario.CurrentRow.Index).Cells(0).Value.ToString

            mdlCRUD.delRecord("tipousuario", "tipousuario_id = '" & strId & "'")

            If (mdlCRUD.blnFlag = True) Then
                Me.refreshList()
            End If
        End If
    End Sub
End Class